package com.lm.liaoming.authority;

import android.content.pm.PackageManager;

import com.lm.liaoming.base.custom.CustomBaseActivity;

import androidx.annotation.NonNull;

public abstract class AuthorityActivity extends CustomBaseActivity {


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//申请全部权限
        if (requestCode == AuthorityType.ALL) {
            boolean hasPermissionDismiss = false;//有权限没有通过
                for (int i = 0; i < grantResults.length; i++) {
                    if (grantResults[i] == -1) {
                        hasPermissionDismiss = true;
                    }
                }
                //如果有权限没有被允许
                if (hasPermissionDismiss) {
                    onRequestPermissionsResult(requestCode, false);
                }else{
                    //全部权限通过，可以进行下一步操作。。。
                    onRequestPermissionsResult(requestCode, true);
                }
        } else {
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                onRequestPermissionsResult(requestCode, true);
            } else {
                onRequestPermissionsResult(requestCode, false);
            }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    /**
     * @param requestCode 权限类型
     * @param yesOrNo     是否申请成功
     */
    public abstract void onRequestPermissionsResult(int requestCode, boolean yesOrNo);
}
